﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BPM.Foundation.WFDB;
using System.Workflow.ComponentModel;

namespace BPM.Base.WFLib.GuideForm
{
	public partial class DForm: Form
    {
        public 等待提交 activity;
        public ITemplate template;

        string dftype = "template";

        public DForm( ITemplate Template)
        {
            InitializeComponent();


            string d = Template.启动时填写的表单;

            template = Template;
            if (d != "")
            {
                string[] ss = d.Split(',');
                List<string> df = new List<string>();

                foreach (string s in ss)
                {
                    df.Add(s.Split('.')[0]);
                }

                foreach (string s in df.Distinct())
                {
                    DF.Items.Add(s);

                    var v = wfs.getWFS().getFormTemplet(s);
                    foreach (var it in v.templetItem)
                    {
                        var l = this.DFItem.Items.Cast<string>().Count(p => p == it.formName + "." + it.title);
                        if (l == 0)
                        {
                            DFItem.Items.Add(it.formName + "." + it.title);
                        }
                    }
                }


                foreach (string s in ss)
                {
                    for (int i = 0; i < DFItem.Items.Count; i++)
                    {
                        if (DFItem.Items[i].ToString() == s)
                        {
                            DFItem.SetItemChecked(i, true);
                        }
                    }
                }
            }

        }



        public DForm(WaitSubmitDesigner designer,string DFtype)
		{
			InitializeComponent();

            dftype = DFtype;
            activity = designer.Activity as 等待提交;

            string d="";

            if (dftype == "see")
            {
                d = activity.办理查看业务表单;
            }
            if (dftype == "edit")
            {
                d = activity.办理添写业务表单;
            }

            if (d != "")
            {
                string[] ss =d.Split(',');
                List<string> df = new List<string>();

                foreach (string s in ss)
                {
                    df.Add(s.Split('.')[0]);
                }

                foreach (string s in df.Distinct())
                {
                    DF.Items.Add(s);

                    var v = wfs.getWFS().getFormTemplet(s);
                    foreach (var it in v.templetItem)
                    {
                        var l = this.DFItem.Items.Cast<string>().Count(p => p == it.formName + "." + it.title);
                        if (l == 0)
                        {
                            DFItem.Items.Add(it.formName + "." + it.title);
                        }
                    }
                }


                foreach (string s in ss)
                {
                    for (int i = 0; i < DFItem.Items.Count; i++)
                    {
                        if (DFItem.Items[i].ToString() == s)
                        {
                            DFItem.SetItemChecked(i, true);
                        }
                    }
                }
            }
           
		}

        private void button_add_Click(object sender, EventArgs e)
        {
            if (this.STDFList.SelectedItem != null)
            {
                var l = this.DF.Items.Cast<string>().Count(p => p == STDFList.SelectedItem.ToString());
                 if (l == 0)
                 {
                     DF.Items.Add(STDFList.SelectedItem.ToString());
                 }
            }
        }

        private void DF_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DF.SelectedItem != null)
            {
               

                var v = wfs.getWFS().getFormTemplet(DF.SelectedItem.ToString());
               foreach (var it in v.templetItem)
               {
                   var l = this.DFItem.Items.Cast<string>().Count(p => p == it.formName + "." + it.title);
                   if (l == 0)
                   {
                       DFItem.Items.Add(it.formName + "." + it.title);
                   }
               }
            }
        }

        private void DForm_Load(object sender, EventArgs e)
        {
            if (Root().数据表单列表 != "")
            {
                string[] ss = Root().数据表单列表.Split(',');

                foreach (string s in ss)
                {
                    this.STDFList.Items.Add(s);
                }
            }
        }

        //[2.4.1]
        ITemplate Root()
        {
            if (template != null)
            {
                return template;
            }

            Activity o = activity.Parent;

            while (o.Parent != null)
            {
                o = o.Parent;
            }
            ITemplate tp = o as ITemplate;

            if (tp != null)
            {
                return tp;
            }
            else
            {
                throw new System.Exception("GetRoot");
            }

        }

        private void button_del_Click(object sender, EventArgs e)
        {
            if (DF.SelectedItem != null)
            {
              
         
            var v = this.DFItem.Items.Cast<string>().Where(p => p.Contains(DF.SelectedItem.ToString() + ".")).ToList();
            System.Console.WriteLine(v.Count);
            foreach (string s in v)
            {
                System.Console.WriteLine(s);
                this.DFItem.Items.Remove(s);
               
            }
              DF.Items.Remove(DF.SelectedItem);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string s = "";
            foreach(var v in this.DFItem.CheckedItems)
            {

                s = s + v.ToString() + ",";
            }
            if (dftype == "see")
            {
                activity.办理查看业务表单 = s.Remove(s.Length - 1);
            }
            if (dftype == "edit")
            {
                activity.办理添写业务表单 = s.Remove(s.Length - 1);
            }
            if (dftype == "template")
            {
                template.启动时填写的表单 = s.Remove(s.Length - 1);
            }
            this.Close();
        }
	}
}
